﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.SharpZipLib;
using System.IO;
using System.Data;

namespace Agile.Engine.Utils
{
    public class ZipUtils
    {
        private static int ZipLevel = 4096;

        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static byte[] Zip(Stream input)
        {
            using (MemoryStream output = new MemoryStream())
            {
                input.Position = 0;
                ICSharpCode.SharpZipLib.BZip2.BZip2.Compress(input, output, false, ZipLevel);
                return output.ToArray();
            }
        }

        /// <summary>
        /// 解压缩
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static byte[] UnZip(Stream input)
        {
            using (MemoryStream output = new MemoryStream())
            {
                input.Position = 0;
                ICSharpCode.SharpZipLib.BZip2.BZip2.Decompress(input, output, false);
                return output.ToArray();
            }
        }

        public static string ZipDataTable(DataTable data)
        {
            DataSet ds = new DataSet();            
            ds.Tables.Add(data.Select().CopyToDataTable());
            return ZipDataSet(ds);
        }

        public static DataTable UnZipDataTable(string base64)
        {
            return UnZipDataSet(base64).Tables[0];
        }

        /// <summary>
        /// 压缩数据
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static string ZipDataSet(DataSet ds)
        {
            using (MemoryStream output = new MemoryStream())
            {
                ///写入
                ds.WriteXml(output, XmlWriteMode.WriteSchema);
                ///压缩
                var compress = Zip(output);
                return Convert.ToBase64String(compress);
            }
        }

        /// <summary>
        /// 解压数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static DataSet UnZipDataSet(string data)
        {
            DataSet ds = new DataSet();
            if (string.IsNullOrEmpty(data))
                return ds;
            var bytes = Convert.FromBase64String(data);
            using (MemoryStream input = new MemoryStream(bytes))
            {
                var unzip = UnZip(input);
                using (MemoryStream s = new MemoryStream(unzip))
                {
                    ds.ReadXml(s, XmlReadMode.ReadSchema);
                    return ds;
                }
            }
        }

        public static byte[] UnZipBase64(string s)
        {
            var d = Convert.FromBase64String(s);
            using (var stream = new MemoryStream(d))
            {
                return UnZip(stream);
            }
        }

        public static string ZipBase64(byte[] b)
        {
            using (var stream = new MemoryStream(b))
            {
                return Convert.ToBase64String(Zip(stream));
            }
        }

    }
}
